package com.yonyou.crm.cum.contact.ctrl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.cum.contact.entity.ContactVO;
import com.yonyou.crm.cum.contact.rmitf.IContactRmService;


@RestController
@RequestMapping(value = "/cum/contacts")
public class ContactCtrl {

	@Autowired
	private IContactRmService service;
	
	/**
	 * 查询联系人
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);

		@SuppressWarnings("unchecked")
		Page<ContactVO> page = service.getList(getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
		
		return ResponseUtil.getObject(page);
	}
	
	private Page<ContactVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {
		Integer page = paramObj.getInt(HttpKeyConstant.Page);
		Integer pageSize = paramObj.getInt(HttpKeyConstant.PageSize);
		if (page == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}
		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}
		return new Page<ContactVO>(page, pageSize);
	}
	
	/**
	 * 联系人详情
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public Object detail(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		if (id == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "id不能为空");
		}		
		return ResponseUtil.getObject(service.getDetail(id));
	}
	
	/**
	 * 新增联系人
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		ContactVO contact = RequestUtil.getVO(request, ContactVO.class);
		String validMsg = validator(contact);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		contact = service.insert(contact);
		return ResponseUtil.getObject(contact);
	}
	
	private String validator(ContactVO vo) {
		if (vo == null) {
			return "转换实体失败";
		}
		if (StringUtils.isBlank(vo.getName())) {
        	return  "姓名不能为空";
		}
		return "";
	}

	/**
	 * 修改联系人
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		ContactVO contact = RequestUtil.getVO(request, ContactVO.class);		
		if (contact == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "转换实体失败");
		}
		contact.setId(id);
		String validMsg = validator(contact);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		ContactVO newContact = service.update(contact);
		return ResponseUtil.getObject(newContact);
	}
	
	/**
	 * 删除联系人
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Long id ,HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		if (id == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "id不能为空");
		}
		service.delete(id);
		return ResponseUtil.getObject();
	}
	
	/**
	 * 批量删除联系人
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		
		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");		
		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}
		String[] idArray = idsStr.split(",");
		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}
		Page<ContactVO> page = service.batchDelete(idArray, getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
		return ResponseUtil.getObject(page);
	}
}
